package com.yy.shardingsphere.controller;

import com.yy.shardingsphere.service.UserService;
import com.yy.shardingsphere.utils.SnowFlake;
import com.yy.shardingsphere.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yangyao
 * @Description: UserController
 * @date 2018/11/1 16:02
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/")
    public void save(@RequestBody User user) {
        Long userId = new SnowFlake(1,1).nextId();
        System.out.println("雪花生成id"+userId);
        user.setUserId(userId);
        this.userService.save(user);
    }

    @GetMapping(value = "/{userId}")
    public User getUserByUserId(@PathVariable("userId") Long userId) {
        return this.userService.getUserByUserId(userId);
    }

    @GetMapping(value = "/")
    public List<User> getAllUsers() {
        return this.userService.getAllUsers();
    }
}
